﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Dne.GridExt.Exceptions;
using Dne.GridExt.Models;

namespace Dne.GridExt
{
    public static class DneGridExtension
    {
        public static string DneGrid(this HtmlHelper helper, DneGrid grid)
        {
            return GridCodeGenerator.GenerateCode(grid);
        }

    }

    public static class GridCodeGenerator
    {
        private const string _BEGIN_SCRIPT = "<script type=\"text/javascript\">";
        private const string _END_SCRIPT = "</script>";

        private const string _BEGIN_JQUERY = "jQuery(document).ready(";
        private const string _END_JQUERY = ");";

        private const string _BEGIN_FUNCTION = "function(){";
        private const string _END_FUNCTION = "}";

        private const string _BEGIN_GRID = ".jqGrid({";
        private const string _END_GRID = "});";

        private const string _JQUERY = "jQuery(\"#{0}\")";
        private const string _TABLE = "\n<table id=\"{0}\">\n</table>";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="grid"></param>
        /// <exception cref="ArgumentRequiredExeption"></exception>
        /// <returns></returns>
        public static string GenerateCode(DneGrid grid)
        {
            var codeBuilder = new StringBuilder();
            codeBuilder.PutBegin(grid);

            codeBuilder.PutUrl(grid);
            codeBuilder.AppendLine("mtype: \"POST\",");
            codeBuilder.AppendLine("height: 200,");
            codeBuilder.AppendLine("datatype: 'json',");
            codeBuilder.PutColumns(grid);
            //TODO:tu inserty propertow

            codeBuilder.PutEnd(grid);

            codeBuilder.PutTable(grid);

            return codeBuilder.ToString();
        }

        private static void PutColumns(this StringBuilder codeBuilder, DneGrid grid)
        {
            codeBuilder.Append("colNames: [");
            int columnCount = grid.Columns.Count();
            for (int i = 0; i < columnCount; i++)
            {
                codeBuilder.AppendFormat("'{0}'", grid.Columns[i].Name);
                if (i != columnCount - 1)
                    codeBuilder.Append(",");
            }
            codeBuilder.AppendLine("],");
            PutColModel(codeBuilder, grid);
        }

        private static void PutColModel(this StringBuilder codeBuilder, DneGrid grid)
        {
            codeBuilder.Append("colModel: [");
            var columnCount = grid.Columns.Count();
            for (int i = 0; i < columnCount; i++)
            {
                codeBuilder.PutColumnModel(grid.Columns[i]);
                if (i != columnCount - 1)
                    codeBuilder.Append(",");
            }

            codeBuilder.AppendLine("]");
        }

        private static void PutColumnModel(this StringBuilder codeBuilder, IColumn column)
        {
            codeBuilder.Append("{");
            codeBuilder.AppendFormat(" name:'{0}'", column.Name);
            codeBuilder.AppendFormat(", index:'{0}'", column.Index);
            codeBuilder.Append("}");
        }

        private static void PutUrl(this StringBuilder codeBuilder, DneGrid grid)
        {
            if (string.IsNullOrEmpty(grid.Url))
                throw new ArgumentRequiredException("Url");
            codeBuilder.AppendFormat("\nurl: '{0}',", grid.Url);
            codeBuilder.AppendLine();
        }

        private static void PutBegin(this StringBuilder codeBuilder, DneGrid grid)
        {
            codeBuilder.AppendLine(_BEGIN_SCRIPT);
            codeBuilder.AppendLine(_BEGIN_JQUERY);
            codeBuilder.AppendLine(_BEGIN_FUNCTION);
            codeBuilder.AppendFormat(_JQUERY, grid.Id);
            codeBuilder.Append(_BEGIN_GRID);
        }

        private static void PutEnd(this StringBuilder codeBuilder, DneGrid grid)
        {
            codeBuilder.AppendLine(_END_GRID);
            codeBuilder.Append(_END_FUNCTION);
            codeBuilder.AppendLine(_END_JQUERY);
            codeBuilder.AppendLine(_END_SCRIPT);
        }

        private static void PutTable(this StringBuilder codeBuilder, DneGrid grid)
        {
            codeBuilder.AppendFormat(_TABLE, grid.Id);
        }
    }
}
